import 'package:cached_network_image/cached_network_image.dart';
import 'package:edu/constants/app_strings.dart';
import 'package:edu/networks/course/models/course_response.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class CourseEduCellWidget extends StatelessWidget {
  final CourseBrief course;
  final GestureTapCallback? onTap;

  const CourseEduCellWidget({super.key, required this.course, this.onTap});

  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap: onTap,
      child: Padding(
        padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
        child: Row(
          children: [
            _buildThumbnailWidget(context),
            const SizedBox(width: 16),
            _buildTextInfoWidget(context),
          ],
        ),
      ),
    );
  }

  // 已购买的课程信息
  Widget _buildTextInfoWidget(BuildContext context) {
    final majorStyle = Theme.of(
      context,
    ).textTheme.titleMedium?.copyWith(fontWeight: FontWeight.bold);
    final minorStyle = Theme.of(
      context,
    ).textTheme.titleSmall?.copyWith(color: Colors.grey[500]);
    return Expanded(
      child: SizedBox(
        height: 125,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            _buildMajorTitle(context, majorStyle),
            const SizedBox(height: 8),
            _buildMinorTyleRow(context, minorStyle),
            const Spacer(),
            _buildCourseVideoInfo(context),
          ],
        ),
      ),
    );
  }

  // 已购买课程的缩略图
  Widget _buildThumbnailWidget(BuildContext context) {
    return ClipRRect(
      borderRadius: BorderRadius.circular(8),
      child: CachedNetworkImage(
        width: 90,
        height: 125,
        fit: BoxFit.cover,
        imageUrl: course.imageUrl,
        errorWidget: (context, url, error) =>
            const Icon(Icons.error, color: Colors.red),
        placeholder: (context, url) =>
            const CupertinoActivityIndicator(radius: 15),
      ),
    );
  }

  // 已购买课程的名称
  Widget _buildMajorTitle(BuildContext context, TextStyle? majorStyle) {
    return Text(
      course.title,
      style: majorStyle,
      maxLines: 2,
      overflow: TextOverflow.ellipsis,
    );
  }

  // 已购买课程的作者和等级
  Widget _buildMinorTyleRow(BuildContext context, TextStyle? minorStyle) {
    return Row(
      children: [
        Text(
          course.author,
          style: minorStyle,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
        const SizedBox(width: 16),
        Text(
          course.level,
          style: minorStyle,
          maxLines: 1,
          overflow: TextOverflow.ellipsis,
        ),
      ],
    );
  }

  // 已购买课程的视频数和时长
  Widget _buildCourseVideoInfo(BuildContext context) {
    final videoCountSpan1 = TextSpan(
      text: AppStrings.videoCount,
      style: Theme.of(context).textTheme.titleSmall,
    );
    final videoCountSpan2 = TextSpan(
      text: course.videoCount.toString(),
      style: Theme.of(
        context,
      ).textTheme.titleSmall?.copyWith(fontWeight: FontWeight.bold),
    );
    final durationSpan1 = TextSpan(
      text: AppStrings.duration,
      style: Theme.of(context).textTheme.titleSmall,
    );
    final durationSpan2 = TextSpan(
      text: course.durationInMinutes.toString(),
      style: Theme.of(
        context,
      ).textTheme.titleSmall?.copyWith(fontWeight: FontWeight.bold),
    );
    final durationSpan3 = TextSpan(
      text: 'm',
      style: Theme.of(context).textTheme.titleSmall,
    );

    return Row(
      children: [
        RichText(text: TextSpan(children: [videoCountSpan1, videoCountSpan2])),
        const Spacer(),
        RichText(
          text: TextSpan(
            children: [durationSpan1, durationSpan2, durationSpan3],
          ),
        ),
      ],
    );
  }
}
